package org.jackysoft.interceptor;

import java.lang.reflect.Method;

import org.apache.commons.beanutils.PropertyUtils;
import org.jackysoft.util.ProxyUtils;

import com.opensymphony.xwork2.ActionInvocation;

public class GetInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1158533224155348115L;

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		
		super.intercept(ai);
		Object action = ai.getAction();

		Object serviceTarget = ProxyUtils.getAopTarget(service);
	  
				
		Method[] beanMethods = bean.getClass().getMethods();

		Method getId = null;

		for (Method m : beanMethods) {			
			if (m.isAnnotationPresent(org.jackysoft.annotations.BID.class)) {
				getId = m;
				break;
			}
		}

		if (getId == null)
			throw new RuntimeException(
					"请在 VO 使用 org.jackysoft.annotations.BID 标注业务主键字段!");

		Object id = getId.invoke(bean);

		//JdkDynamicAopProxy jda = service;
		
		Method 	serviceGetBean = serviceTarget.getClass().getMethod(SERVICE_GET_BEAN_,id.getClass());

		// 从数据库查询得到的对象
		
		Object actionBean = serviceGetBean.invoke(serviceTarget, id);

		PropertyUtils.setProperty(action, "bean", actionBean);
		
		return ai.invoke();
	}

}
